package com.huarui.dictionary.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huarui.dictionary.DictionaryEnum;
import com.huarui.dictionary.dao.IDictionaryDao;
import com.huarui.dictionary.model.Dictionary;

public class DictionaryReadOnlyService {

	private Map<String, Dictionary> dictionaryMap = null;

	private List<Dictionary> dictionaryList;

	private IDictionaryDao dictionaryDao;

	public void init() throws Exception {
		dictionaryList = dictionaryDao.getAll();
		initDictionaryMap();
	}

	static public class DictionaryResource {
		protected static final Map<String, String> dictionaryMap = new HashMap<String, String>();
		
		public static String getDictionaryValue(DictionaryEnum eum, String varIndex) {
			return dictionaryMap.get(eum.getGroup().concat(varIndex));
		}
	}
	
	private void initDictionaryMap() {
		dictionaryMap = new HashMap<String, Dictionary>();
		for (Dictionary d : dictionaryList) {
			dictionaryMap.put(d.getVarGroup() + d.getVarIndex(), d);
			DictionaryResource.dictionaryMap.put(d.getVarGroup() + d.getVarIndex(), d.getVarName());
		}
	}

	/**
	 * 获取字典值
	 * 
	 * @param group
	 *            组别
	 * @param varIndex
	 *            编号
	 * @return
	 */
	public String getDictionaryValue(DictionaryEnum group, String varIndex) {

		Dictionary d = dictionaryMap.get(group.getGroup().concat(varIndex));

		if (d != null) {
			return d.getVarName();
		}

		return "";
	}

	/**
	 * 获取字典值
	 * 
	 * @param group
	 *            组别
	 * @param varIndex
	 *            编号
	 * @return
	 */
	public String getDictionaryValue(DictionaryEnum group, Integer varIndex) {

		return getDictionaryValue(group, String.valueOf(varIndex));
	}

	/**
	 * 获取一组字典值
	 * 
	 * @param group
	 * @return
	 */
	public List<Dictionary> getDictionaryByGroup(DictionaryEnum group) {

//		List<Dictionary> list = new ArrayList<Dictionary>();
//
//		for (Dictionary d : dictionaryList) {
//			if (d.getVarGroup().equalsIgnoreCase(group.getGroup())) {
//				list.add(d);
//			}
//		}
		return this.getDictionaryByGroup(group.getGroup());

	}
	
	public List<Dictionary> getDictionaryByGroup(String group) {

		List<Dictionary> list = new ArrayList<Dictionary>();
		for (Dictionary d : dictionaryList) {
			if (d.getVarGroup().equalsIgnoreCase(group)) {
				list.add(d);
			}
		}
		return list;

	}

	public void setDictionaryDao(IDictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}

	/**
	 * 后台查询
	 * @param paramsMap
	 * @return
	 */
	public Map<String, Object> listDictionaryByComplex(Map<String, Object> paramsMap) {
		Integer totalProperty = dictionaryDao.getDictionaryListCountByComplex(paramsMap);
		List<Map<String, Object>> dictionaryList = dictionaryDao.listDictionaryByComplex(paramsMap);
		paramsMap.clear();
		paramsMap.put("totalProperty", totalProperty);
		paramsMap.put("root", dictionaryList);
		return paramsMap;
	}
}
